﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace 颜色值转换_十六进制转数字_
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        #region 主体方法

        /// <summary>
        /// 十六进制转RGB数值
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static (int, int, int, Color) HexToColor(string hex = "#FFFFFF")
        {
            if (hex.StartsWith("#"))
            {
                hex = hex.Substring(1); // 去掉开头的 #
            }

            // 确保输入的字符串长度为6位
            if (hex.Length != 6)
            {
                throw new ArgumentException("The hexadecimal color must be 6 characters long.");
            }

            // 解析每种颜色成分
            int r = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
            int g = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
            int b = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);

            return (r, g, b, Color.FromArgb(255, (byte)r, (byte)g, (byte)b));
        }

        /// <summary>
        /// 颜色值转换为十六进制
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static string RgbToHex(Color color)
        {
            // 将每个颜色分量转换为十六进制字符串，并补全为两位
            string redHex = color.R.ToString("X2");
            string greenHex = color.G.ToString("X2");
            string blueHex = color.B.ToString("X2");

            // 合并三个颜色分量
            return "#" + redHex + greenHex + blueHex;
        }

        /// <summary>
        /// 将给定的 RGB 值转换为十六进制字符串。
        /// </summary>
        /// <param name="red">红色分量（0-255）</param>
        /// <param name="green">绿色分量（0-255）</param>
        /// <param name="blue">蓝色分量（0-255）</param>
        /// <returns>十六进制颜色字符串（格式为 #RRGGBB）</returns>
        /// <exception cref="ArgumentOutOfRangeException">传入数据超出指定范围</exception>
        public static (string, Color) RgbToHex(int red = 0, int green = 0, int blue = 0)
        {
            // 确保输入的值在有效范围内
            if (red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255)
            {
                throw new ArgumentOutOfRangeException("RGB 值必须在 0 到 255 之间。");
            }

            // 将每个颜色分量转换为十六进制字符串，并补全为两位
            string redHex = red.ToString("X2");
            string greenHex = green.ToString("X2");
            string blueHex = blue.ToString("X2");

            // 合并三个颜色分量，并加上前缀 #
            return ("#" + redHex + greenHex + blueHex, Color.FromArgb(255, (byte)red, (byte)green, (byte)blue));
        }

        #endregion

        public MainWindow()
        {
            InitializeComponent();
        }

        private void ColorText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (!string.IsNullOrEmpty(ColorText.Text))
                {
                    var t = HexToColor(this.ColorText.Text);
                    this.ShowA.Content = t.Item1 + "," + t.Item2 + "," + t.Item3;
                    this.ColorPrivewA.Background = new SolidColorBrush(t.Item4);
                }
            }
        }

        private void ColorNumber_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (!string.IsNullOrEmpty(ColorNumber.Text))
                {
                    // 分割字符串
                    string[] parts = this.ColorNumber.Text.Split(',');
                    var t = RgbToHex(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]));
                    this.ShowB.Content = t.Item1;
                    this.ColorPrivewB.Background = new SolidColorBrush(t.Item2);
                }
            }
        }
    }
}
